package com.jinlejia.erp.ui.home;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.freedom.yefeng.yfrecyclerview.YfLoadMoreListener;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.NoticeAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityNoticeListBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by Administrator on 2017/8/21 0021.
 */

public class NoticeListActivity extends AppCompatActivity implements YfLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private NoticeAdapter adapter;
    private JSONObject jsonObject = new JSONObject();
    private int page = 1;
    private boolean mLoadingLock = false;
    private ActivityNoticeListBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_notice_list);
        binding.backBtn.setOnClickListener(v->finish());
        binding.swipeRefresh.setColorSchemeResources(R.color.yellow, R.color.light_yellow);
        binding.swipeRefresh.setOnRefreshListener(this);
        binding.yfRecycler.setHasFixedSize(true);
        binding.yfRecycler.setLayoutManager(new LinearLayoutManager(this));
        binding.yfRecycler.enableAutoLoadMore(this);
        ArrayList<Map<String, Object>> data = new ArrayList<>();
        adapter = new NoticeAdapter(data);

        binding.yfRecycler.setAdapter(adapter);
        adapter.changeMode(YfListMode.MODE_LOADING);

        adapter.setOnItemClickListener((view, o) -> {
            Map<String,Object> map = (Map<String, Object>) o;
            Intent intent = new Intent(NoticeListActivity.this,NoticeDetailActivity.class);
            intent.putExtra("id",map.get("id").toString());
            startActivity(intent);
        });
        getData();

    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void loadMore() {
        if (adapter.getData().size() % 10 == 0) {
            if (mLoadingLock) {
                return;
            }
            mLoadingLock = true;
            page++;
            adapter.addFooter("loading");

            getData();
        }else{
            adapter.addFooter(R.string.is_all);
        }
    }

    /**
     * 获取数据
     */
    private void getData() {
        mLoadingLock = true;
        jsonObject.put("pageSize", 20);
        jsonObject.put("pageIndex", page);
        OkHttpUtils
                .postString()
                .url(ApiConfig.NOTICE_LIST)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(NoticeListActivity.this, response, adapter)) {
                            //数据列表
                            ArrayList<Map<String, Object>> list = JSON.parseObject(response.get("data").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            if (page == 1) {
                                adapter.setData(list);
                            } else {
                                adapter.addData(list);
                            }
                        } else if (response.get("code").toString().equals("303")) {
                            //没有更多
                            page--;
                        } else if (response.get("code").toString().equals("501")) {
                            adapter.changeMode(YfListMode.MODE_EMPTY);
                        }
                        if (binding.swipeRefresh.isRefreshing()) {
                            binding.swipeRefresh.setRefreshing(false);
                        }
                        mLoadingLock = false;
                    }
                });
    }

}
